import * as echarts from '../../../ec-canvas/echarts.js';
const outSide = {
  rpx: '',
  // xInfo: [0,0,0,0,0],
  // yInfo: [0,0,0,0,0],
  xInfo: wx.getStorageSync('xInfo'),
  yInfo: wx.getStorageSync('yInfo'),
}
const app = getApp();
const time = require('../../../utils/util.js');
Page({
  data: {

    ecBar: {
      // 如果想要禁止触屏事件，以保证在图表区域内触摸移动仍能滚动页面，
      // 就将 disableTouch 设为 true
      // disableTouch: true,

      onInit: function(canvas, width, height) {
        // console.log(375 * outSide.rpx)
        const lineChart = echarts.init(canvas, null, {
          width: 340 * outSide.rpx,
          height: 320 * outSide.rpx
        });

        canvas.setChart(lineChart);
        // lineChart.setOption(getBarOption(outSide.xInfo, outSide.yInfo));
        lineChart.setOption(getBarOption(wx.getStorageSync('xInfo'), wx.getStorageSync('yInfo')));
        return lineChart;
      }
      
    },
    //主要信息
    balance: [],
    code: '',
    extract_time: 0,
    receive_time: 0,

    //参数
    rpx: '',
    page: 1,

    //弹框
    flag_rule: true,
    flag_ruleTwo: true,
  },
  //去明细
  go_detail(){
    wx.navigateTo({
      url: '../../payment/bodou/bodouDetail/bodouDetail'
    })
  },
  //七日收益
  earns() {
    wx.request({
      url: app.globalData.url + 'api/task/seven/day/earnings',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let earnsInfo = e.data.data.earnings;
        var xInfo = [],
          yInfo = [];
        earnsInfo.forEach(function(value, index) {
          var str = value.show_date;
          var str_new = str.replace(/"-"/, ".");
          var str_re = str_new.substring(5);
          value.show_date = str_re;
          value.operate_num = parseInt(value.operate_num);
          xInfo.push(value.show_date);
          yInfo.push(value.operate_num)
        })
        outSide.xInfo = xInfo;
        outSide.yInfo = yInfo;

        console.log(outSide.xInfo)
        console.log(outSide.yInfo)
      }
    })
  },
  getRpx() {
    wx.getSystemInfo({
      success: (e) => {
        var rpx = e.windowWidth / 375;
        outSide.rpx = rpx
        this.setData({
          rpx: rpx,
        })
      }
    })
  },
  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_share: true,
    })
  },
  //加载个人信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let info = e.data.data,
          realMoney = parseInt(info.bb) ? parseInt(info.bb) / this.data.balance.bb_rate : 0;
        this.setData({
          info: info,
          realMoney: realMoney,
        })
      },
    })
  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
      flag_ruleTwo: true,
    })
  },
  //点击 提现
  change_withdraw() {
    wx.showToast({
      title: '兑换波豆功能即将上线',
      icon: 'none',
    })
  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let balance = e.data.data;
        let code = e.data.data.code;
        that.setData({
          balance: balance,
          code: code,
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRpx();
    this.earns();
    this.balance();
    this.getInfo();
    console.log("xInfo+"+wx.getStorageSync('xInfo'))
    console.log("yInfo+"+wx.getStorageSync('yInfo'))
  },
});

function getBarOption(_xInfo, _yInfo) {
  return {
    color: ["#d0a76c"],
    grid: {
      x: '0', //相当于距离左边效果:padding-left
      y: '10%', //相当于距离上边效果:padding-top
      // x2: '10',
      // y2: '10',
      bottom: '30%',
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: 'axis',
      position: ['10%', 0],
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: '#d0a76c',
        },
        textStyle: {
          color: '#d0a76c'
        }
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      nameGap: 0,
      data: _xInfo,
      axisLine: {
        lineStyle: {
          color: "#dedede"
        },
        show: false
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        fontSize: 10,
      }
    },
    yAxis: {
      x: 'center',
      type: 'value',
      show: true,
      splitLine: {
        lineStyle: {
          type: 'solid',
          color: 'rgba(0,0,0,.1)'
        }
      },
      axisLine: {
        lineStyle: {
          color: "#dedede"
        },
        show: false
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        fontSize: 10,
      },
    },
    series: [{
      name: '收益',
      type: 'line',
      areaStyle: {
        opacity: .1,
      },
      smooth: false,
      data: _yInfo,
      itemStyle: {
        normal: {
          label: {
            show: false
          }
        }
      },
      lineStyle: {
        width: 1
      }
    }]
  };
}